package com.lc.user.express.app;


import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;

import com.bumptech.glide.request.target.ViewTarget;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.lc.user.express.R;
import com.lc.user.express.utils.GsonUtil;
import com.zcx.helper.app.AppApplication;
import com.zcx.helper.http.Http;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilSDCard;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import java.util.concurrent.TimeUnit;


public class AppContext extends AppApplication {
    private HttpClient httpClient;
    public static GsonUtil gsonUtil;
    public static String curVersion;
    public static AppContext mContext;
    public static com.zcx.helper.scale.ScaleScreenHelper ScaleScreenHelper;
    public static MyPreferences myPreferences;

    @Override
    public void onCreate() {
        super.onCreate();
        gsonUtil = new GsonUtil();
        if (mContext == null) mContext = this;
        initField();
        //Helper.Initialize(this, "Express");
        ScaleScreenHelper = ScaleScreenHelperFactory.create(this, 720);
        myPreferences = new MyPreferences(this);
        Http.getInstance().setIsLog(true);
//        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
//        JPushInterface.init(this);            // 初始化 JPush

       // CrashReport.initCrashReport(getApplicationContext(), "900022235", false);//崩溃日志
        Fresco.initialize(this);
        Http.getInstance().setIsLog(true);        Http.getInstance().setConnectTimeout(60, TimeUnit.SECONDS);       Http.getInstance().setReadTimeout(60, TimeUnit.SECONDS);
        Http.getInstance().setWriteTimeout(60, TimeUnit.SECONDS);
        Http.getInstance().setCache(this, UtilSDCard.getSDCardPath() + "zjdb", 10 * 1024);
        Http.getInstance().setOnStartEndCreateDialog(new Http.OnStartEndCreateDialog() {
            @Override
            public Dialog onCreate(Context context) {
//                return new ProgressDialog(context, R.style.AppDialog);
                android.app.ProgressDialog progressDialog = new android.app.ProgressDialog(context, R.style.AppDialog);

                progressDialog.setCancelable(false);
                return progressDialog;
            }

            @Override
            public void show(Dialog dialog) {
                dialog.show();
            }

            @Override
            public void dismiss(Dialog dialog) {
                dialog.dismiss();
            }
        });
        ViewTarget.setTagId(R.id.glide_tag);
        //初始化GLide图片加载框架

    }

    private void initField() {
        httpClient = this.createHttpClient();
        // 当前版本名
        try {
            curVersion = getApplicationContext().getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName;
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static AppContext getContext() {
        return mContext;
    }
       @Override
    public void onLowMemory() {
        super.onLowMemory();
        this.shutdownHttpClient();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        this.shutdownHttpClient();
    }

    // 创建HttpClient实例
    private HttpClient createHttpClient() {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setUseExpectContinue(params, true);
        HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
        HttpConnectionParams.setSoTimeout(params, 20 * 1000);
        HttpConnectionParams.setSocketBufferSize(params, 8192);
        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);

        return new DefaultHttpClient(connMgr, params);
    }

    // 关闭连接管理器并释放资源
    private void shutdownHttpClient() {
        if (httpClient != null && httpClient.getConnectionManager() != null) {
            httpClient.getConnectionManager().shutdown();
        }
    }

    // 对外提供HttpClient实例
    public HttpClient getHttpClient() {
        return httpClient;
    }

}
